package com.hwq.sql.tool.model;

import com.hwq.sql.tool.enums.DataType;
import com.hwq.sql.tool.utils.ExcelUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.xssf.usermodel.XSSFRow;

@Getter
@Setter
public class Field {

    private String name;        // 字段名
    private String dataType;    // 数据类型
    private String len;         // 数据长度
    private String comment;     // 字段注释
    private Boolean pk;         // 是否主键
    private Boolean noNull;     // 是否非空
    private String index;       // 索引
    private String uq;          // 唯一索引
    private String defValue;    // 默认值
    private String fieldType;   // 字段类型 初始、新增、替换
    private String reField;     // 替换字段
    private Boolean remove;     // 替换后是否移除原字段
    private String version;     // 版本号

    private Boolean isNumber;   // 数据类型是否数字

    public Field(XSSFRow row, Config config) {
        name      = ExcelUtil.getCellValue(row.getCell(0));
        dataType  = ExcelUtil.getCellValue(row.getCell(1));
        len       = ExcelUtil.getCellValue(row.getCell(2));
        comment   = ExcelUtil.getCellValue(row.getCell(3));
        pk        = "是".equals(ExcelUtil.getCellValue(row.getCell(4)));
        noNull    = "是".equals(ExcelUtil.getCellValue(row.getCell(5)));
        index     = ExcelUtil.getCellValue(row.getCell(6));
        uq        = ExcelUtil.getCellValue(row.getCell(7));
        defValue  = ExcelUtil.getCellValue(row.getCell(8));
        fieldType = ExcelUtil.getCellValue(row.getCell(9));
        reField   = ExcelUtil.getCellValue(row.getCell(10));
        remove    = "是".equals(ExcelUtil.getCellValue(row.getCell(11)));
        version   = ExcelUtil.getCellValue(row.getCell(11));

        DataType type = DataType.get(this.dataType, config.getDatabase());
        isNumber = type == DataType.INT || type == DataType.NUMBER;
    }

}
